package com.ctyon.watchmanager.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import com.ctyon.watchmanager.R;
import com.ctyon.wheelpicker.base.WheelItem;
import com.ctyon.wheelpicker.base.WheelItemView;

/**
 * CreateDate：19-7-19 on 下午4:10
 * Describe:
 * Coder: lee
 */
public class PickerDialog extends Dialog {

    private OnPickerCallback listener;
    private WheelItemView hourPickerBegin;
    private WheelItemView minutePickerBegin;
    private WheelItemView hourPickerEnd;
    private WheelItemView minutePickerEnd;
    private TextView tvCancel;
    private TextView tvSubmit;

    private String hb, mb, he, me;

    public PickerDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_picker);
        hourPickerBegin = findViewById(R.id.picker_hour_begin);
        hourPickerBegin.setOnSelectedListener((context, selectedIndex) -> hb = selectedIndex < 10 ? "0" + selectedIndex : "" + selectedIndex);

        minutePickerBegin = findViewById(R.id.picker_minute_begin);
        minutePickerBegin.setOnSelectedListener((context, selectedIndex) -> mb = selectedIndex < 10 ? "0" + selectedIndex : "" + selectedIndex);

        hourPickerEnd = findViewById(R.id.picker_hour_end);
        hourPickerEnd.setOnSelectedListener((context, selectedIndex) -> he = selectedIndex < 10 ? "0" + selectedIndex : "" + selectedIndex);

        minutePickerEnd = findViewById(R.id.picker_minute_end);
        minutePickerEnd.setOnSelectedListener((context, selectedIndex) -> me = selectedIndex < 10 ? "0" + selectedIndex : "" + selectedIndex);

        tvCancel = findViewById(R.id.picker_cancel);
        tvCancel.setOnClickListener(v -> cancel());

        tvSubmit = findViewById(R.id.picker_submit);
        tvSubmit.setOnClickListener(v -> {
            if (null != listener) {
                cancel();
                listener.dataCallback(hb, mb, he, me);
            }
        });

        loadHour();
        loadMinute();
    }

    public void setListener(OnPickerCallback listener) {
        this.listener = listener;
    }

    public interface OnPickerCallback {
        void dataCallback(String hourB, String minuteB, String hourE, String minuteE);
    }

    private void loadHour() {
        WheelItem[] items = new WheelItem[24];
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                items[i] = new WheelItem("0" + i);
            } else {
                items[i] = new WheelItem("" + i);
            }
        }
        hourPickerBegin.setItems(items);
        hourPickerBegin.setSelectedIndex(0);
        hourPickerEnd.setItems(items);
        hourPickerEnd.setSelectedIndex(0);
    }

    private void loadMinute() {
        WheelItem[] items = new WheelItem[60];
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                items[i] = new WheelItem("0" + i);
            } else {
                items[i] = new WheelItem("" + i);
            }
        }
        minutePickerBegin.setItems(items);
        minutePickerBegin.setSelectedIndex(0);
        minutePickerEnd.setItems(items);
        minutePickerEnd.setSelectedIndex(0);
    }

    public void setTime(int hourB, int minuteB, int hourE, int minuteE) {
        hourPickerBegin.setSelectedIndex(hourB);
        minutePickerBegin.setSelectedIndex(minuteB);
        hourPickerEnd.setSelectedIndex(hourE);
        minutePickerEnd.setSelectedIndex(minuteE);
    }

}
